package com.tool.ui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
import javax.swing.filechooser.FileFilter;

import com.tool.LicenseMgmt;
import com.tool.ResourceBundleHelper;

/**
 * License控制展示界面
 * 
 * @author Evan.Hu
 * 
 */
public class LicenseUI extends CoreUI {

	private static final long serialVersionUID = 5977763346548128018L;

	JLabel getLicenseLabel;
	JLabel mailToCoderLabel;

	JLabel macLabel;
	JTextField macTxt;

	JLabel invalidDateLabel;
	JTextField invalidDateTxt;

	JButton btnImport;

	public LicenseUI() {
		super();
		setPreferredSize(new Dimension(360, 240));
		initUI();
	}

	private void initUI() {
		resHelper = new ResourceBundleHelper(LicenseUI.class.getName());

		setTitle(resHelper.getString("Frame.title"));
		final MatteBorder bottomLineBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY);

		setLayout(null);

		// 获取license
		getLicenseLabel = new JLabel(resHelper.getString("getLicenseLabel"));
		getLicenseLabel.setBounds(new Rectangle(20, 20, 100, 25));
		getLicenseLabel.setBorder(bottomLineBorder);
		add(getLicenseLabel, null);

		mailToCoderLabel = new JLabel(resHelper.getString("mailToCoderLabel"));
		mailToCoderLabel.setBounds(new Rectangle(125, 20, 160, 25));
		mailToCoderLabel.setForeground(Color.BLUE);
		mailToCoderLabel.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e) {
				try {
					Desktop.getDesktop().mail(new URI("mailto:" + mailToCoderLabel.getText()));
				} catch (IOException e1) {
				} catch (URISyntaxException e1) {
					e1.printStackTrace();
				}
			}

			public void mouseEntered(MouseEvent e) {
				setCursor(Cursor.getPredefinedCursor(12));
			}

			public void mouseExited(MouseEvent e) {
				setCursor(Cursor.getPredefinedCursor(0));
			}

			public void mousePressed(MouseEvent e) {
			}

			public void mouseReleased(MouseEvent e) {

			}
		});
		add(mailToCoderLabel, null);

		// 特征码
		macLabel = new JLabel(resHelper.getString("macLabel"));
		macLabel.setBounds(new Rectangle(20, 50, 100, 25));
		macLabel.setBorder(bottomLineBorder);
		add(macLabel, null);

		macTxt = new JTextField();
		macTxt.setBounds(new Rectangle(125, 50, 160, 25));
		add(macTxt, null);

		// 失效日期
		invalidDateLabel = new JLabel(resHelper.getString("invalidDateLabel"));
		invalidDateLabel.setBounds(new Rectangle(20, 80, 100, 25));
		invalidDateLabel.setBorder(bottomLineBorder);
		add(invalidDateLabel, null);

		invalidDateTxt = new JTextField();
		invalidDateTxt.setBounds(new Rectangle(125, 80, 160, 25));
		add(invalidDateTxt, null);

		btnImport = new JButton(new ImportLicenseAction());
		btnImport.setText(resHelper.getString("btnImport"));
		btnImport.setBounds(new Rectangle(20, 120, 160, 25));
		add(btnImport, null);

	}

	public static void main(String[] args) {
		LicenseUI ui = new LicenseUI();
		ui.showUI();
	}

	protected void importLicense_ActionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

		int result = fc.showOpenDialog(getWindow());
		if (result == JFileChooser.APPROVE_OPTION) {
			LicenseMgmt.importLicenseData(fc.getSelectedFile().getPath());
			// 成功导入文件后，可以刷新当前信息
			JOptionPane.showMessageDialog(getWindow(), "License导入成功");
			loadLicenseData();
		}
	}

	protected void onLoad() {
		setResizable(false);

		loadLicenseData();
	}

	private void loadLicenseData() {
		macTxt.setText(LicenseMgmt.getLocalMacCode());
		invalidDateTxt.setText(LicenseMgmt.getValidEndDate());
	}

	class ImportLicenseAction extends AbstractAction {

		protected ImportLicenseAction() {
			super("ImportLicenseAction");
		}

		public void actionPerformed(ActionEvent e) {
			try {
				importLicense_ActionPerformed(e);
			} catch (Exception e2) {
				handUIException(e2);
			}
		}
	}
}
